const dataPrx = require('./database');
const fs = require("fs");
var path = require('path');
const TassCmd = require('./tass_cmd');

var Controller = Controller || {};

Controller.queryVpnStat = async(req, res) => {
    return TassCmd.vpn_stat();
}

Controller.queryCertList = async(req, res) => {
    let data = await dataPrx.knex()
                        .from('t_tass')
                        .select('*');
    return data;
}

Controller.editCert = async(req, res) => {
    let ret = TassCmd.gen_csr(req.body.type == 1, Number(req.body.signIndex), req.dn, path.resolve(__dirname, 'public/certs', req.body.signCsr));
    if(ret == 0) {
        await dataPrx.knex()
        .from('t_tass')
        .update({
            dn: req.body.dn,
            signIndex: Number(req.body.signIndex),
            signCsr: path.join('certs', req.body.signCsr),
        })
        .where({
            type: req.body.type
        });
        return path.join('certs', req.body.signCsr);
    } else {
        return "";
    }
}

Controller.openVpn = async(req, res) => {
    let client_conf = await dataPrx.knex()
                            .from('t_tass')
                            .select('*')
                            .where({type:1})
                            .first();
    let server_conf = await dataPrx.knex()
                            .from('t_tass')
                            .select('*')
                            .where({type:2})
                            .first();
    return TassCmd.open_vpn(req.body.remoteIp, req.body.remotePort, req.body.localIp, req.body.localPort,
        path.resolve(__dirname, 'public', client_conf.signCert), client_conf.signIndex, path.resolve(__dirname, 'public', client_conf.encCert), client_conf.encIndex,
        path.resolve(__dirname, 'public', server_conf.signCert), server_conf.signIndex, path.resolve(__dirname, 'public', server_conf.encCert), server_conf.encIndex);
}

Controller.closeVpn = async(req, res) => {
    return TassCmd.shutdown_vpn();
}

Controller.importCerts = async(req, res) => {
    let file1 = req.files.filter((item)=>item.fieldname == 'file1')[0];
    let file2 = req.files.filter((item)=>item.fieldname == 'file2')[0];
    let file3 = req.files.filter((item)=>item.fieldname == 'file3')[0];
    let path_signCert = path.resolve(__dirname, 'public/certs', file1.originalname);
    let path_encCert = path.resolve(__dirname, 'public/certs', file2.originalname);
    let path_p7b = path.resolve(__dirname, 'public/certs', file3.originalname);
    fs.writeFileSync(path_signCert, file1.buffer);
    fs.writeFileSync(path_encCert, file2.buffer);
    fs.writeFileSync(path_p7b, file3.buffer);

    await dataPrx.knex()
        .from('t_tass')
        .update({
            signIndex: Number(req.body.signIndex),
            encIndex: Number(req.body.encIndex),
            signCert: path.join('certs', file1.originalname),
            encCert: path.join('certs', file2.originalname),
            p7b: path.join('certs', file3.originalname),
        })
        .where({
            type: req.body.type
        });

    return TassCmd.import_files(req.body.type == 1, path_p7b, path_signCert, req.body.signIndex, path_encCert, req.body.encIndex);
}

module.exports = Controller;